package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.PmAdjustManager;
import com.example.demo.entity.PmEmployee;
import com.example.demo.mapper.PmAdjustManagerMapper;
import com.example.demo.mapper.PmEmployeeMapper;
import com.example.demo.service.impl.PmAdjustManagerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/adjust")
public class PmAdjustManagerController extends BaseController{
    @Resource
    PmEmployeeMapper pmEmployeeMapper;
    @Resource
    PmAdjustManagerMapper pmAdjustManagerMapper;
    @Autowired
    PmAdjustManagerServiceImpl pmAdjustManagerService;

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @PostMapping
    public synchronized Result<?> saveOrUpdate(@RequestBody PmAdjustManager pmAdjustManager) {
        PmEmployee pmEmployee = pmEmployeeMapper.selectById(pmAdjustManager.getEmployeeId());
        pmAdjustManager.setName(pmEmployee.getName());
        pmAdjustManager.setSex(pmEmployee.getSex());
        pmAdjustManager.setBirth(pmEmployee.getBirth().substring(0, 7));
        pmAdjustManager.setCompany(pmEmployee.getCompany());
        pmAdjustManager.setOldDepartment(pmEmployee.getDepartment());
        pmAdjustManager.setOldPosition(pmEmployee.getPosition());
        pmAdjustManager.setModifyTime(formatter.format(new Date()));
        pmAdjustManagerService.saveOrUpdate(pmAdjustManager);

        pmEmployee.setPosition(pmAdjustManager.getNowPosition());
        pmEmployee.setDepartment(pmAdjustManager.getNowDepartment());
        pmEmployee.setPositionStatus(pmAdjustManager.getPositionStatus());
        pmEmployeeMapper.updateById(pmEmployee);

        return Result.success();
    }

    @GetMapping("/{employeeId}")
    public synchronized Result<?> get(@PathVariable Long employeeId) {
        return Result.success(pmAdjustManagerMapper.get(employeeId));
    }

    @GetMapping("/list")
    public synchronized Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "10") Integer pageSize,
                                           @RequestParam(defaultValue = "") String search,
                                           @RequestParam(defaultValue = "") Long employeeId,
                                           @RequestParam(defaultValue = "") String company) {
        LambdaQueryWrapper<PmAdjustManager> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(PmAdjustManager::getName, search);
        }
        if (StrUtil.isNotBlank(company) && !company.equals("会博集团及下属子公司")) {
            wrapper.eq(PmAdjustManager::getCompany, company);
        }
        if (employeeId != null) {
            wrapper.eq(PmAdjustManager::getEmployeeId, employeeId);
        }
        Page<PmAdjustManager> pmEmployeePage = pmAdjustManagerMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(pmEmployeePage);
    }
}
